package com.huang.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.huang.domain.SysMenu;
import com.huang.service.SysMenuService;
import com.huang.mapper.SysMenuMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 *
 */
@Service
public class SysMenuServiceImpl extends ServiceImpl<SysMenuMapper, SysMenu>
    implements SysMenuService{

    @Autowired
    private SysMenuMapper sysMenuMapper;

    @Override
    public List<SysMenu> loadSysUserMenus(Long userId) {
        List<SysMenu> menus  =sysMenuMapper.loadSysUserMenus(userId);

        return transToTree(menus,0L);
    }

    private List<SysMenu> transToTree(List<SysMenu> menus,Long pid) {
        //获取第一层级的菜单
        List<SysMenu> roots = menus.stream()
                .filter(m -> m.getParentId().equals(pid))
                .collect(Collectors.toList());

        roots.forEach(root->root.setList(transToTree(menus,root.getMenuId())));

        return roots;
    }
}




